package com.zh.mediaservice.controller.admin;

import com.zh.mediacommon.constant.ReserveConstant;
import com.zh.mediacommon.result.Result;
import com.zh.mediacommon.util.DepartmentMapUtil;
import com.zh.mediapojo.entity.InitDoctorAccount;
import com.zh.mediaservice.mapper.DoctorLoginMapper;
import com.zh.mediaservice.mapper.DoctorMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class InitController {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    DoctorMapper doctorMapper;
    @Autowired
    DoctorLoginMapper doctorLoginMapper;
    @GetMapping("/init")
    public Result init() {
        HashOperations hashOperations = redisTemplate.opsForHash();
        List<Integer> ids = doctorMapper.getDoctorId();
//        for (Integer id : ids){
//        redisTemplate.delete(id.toString());
//        }
        for (Integer id : ids) {
            hashOperations.put(id.toString(), ReserveConstant.MAX_NUM_AM, 6);
            hashOperations.put(id.toString(), ReserveConstant.NOW_NUM_AM, 0);
            hashOperations.put(id.toString(), ReserveConstant.MAX_NUM_PM, 8);
            hashOperations.put(id.toString(), ReserveConstant.NOW_NUM_PM, 0);
            System.out.println(id);
        }
        return Result.success();
    }
    @GetMapping("/initDoctorAccount")
    public Result initDoctorAccount(){
        HashOperations hashOperations = redisTemplate.opsForHash();
        List<InitDoctorAccount> ids = doctorMapper.getDoctorAccount();
        for (InitDoctorAccount id : ids) {
            String t=id.getHid()+""+id.getSid()+""+id.getDid();
//            System.out.println(t);
//            System.out.println(Long.parseLong(t));
            doctorLoginMapper.init(Long.parseLong(t),id.getDid());
        }
        return Result.success();
    }
    @PostMapping("/initDepartment")
    public Result initDepartment(){
                Map<String, List<String>> departmentKeywords= (Map<String, List<String>>) redisTemplate.opsForValue().get("department::key");
        if(departmentKeywords==null)
            departmentKeywords=new HashMap<>();
        departmentKeywords.putAll(DepartmentMapUtil.getDepartmentMap());
        redisTemplate.opsForValue().set("department::key",departmentKeywords);
        return Result.success();
    }

}
